import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
import { Core } from "./core";
import { TransformInterceptor } from "./global/transform/transform.interceptor";
import { HttpExceptionFilter } from "./global/http-exception/http-exception.filter";
import { config } from 'dotenv';
import { NestExpressApplication } from '@nestjs/platform-express';
import { join } from 'path';
import { ValidationPipe } from '@nestjs/common';
import { json, urlencoded } from 'express';

// import { CozeAPI } from '@coze/api';
// const token = 'pat_hDLUMqRTuu6iyZo25QTCKqK07KkRwh6JiQiroJZSUqdKlDnwrMF6PE1LI5POPB70'; //2025/3/1过期
// const workflow_id = '7466775571867959346';

// const apiClient = new CozeAPI({
//   token: token,
//   baseURL: 'https://api.coze.cn'
// });

// setTimeout(() => {
//     onSong()
// }, 3000);

// async function onSong() {
//     const res = await apiClient.workflows.runs.stream({
//         workflow_id: workflow_id,
//         parameters: {
//             input: "可爱的小猫咪"
//         },
//     })
//     console.log("####", res)
// }

// 确保在使用环境变量之前加载 .env
config();

Core.ins.connect();

async function bootstrap() {
    const app = await NestFactory.create<NestExpressApplication>(AppModule);
    
    // 添加全局路由前缀
    app.setGlobalPrefix('uni-record');
    
    // 使用环境变量中的 ASSETS_URL 配置
    app.useStaticAssets(process.env.FILE_PATH, {
        prefix: '/uploads'
    });
    
    app.enableCors();
    app.useGlobalPipes(new ValidationPipe());
    app.useGlobalInterceptors(new TransformInterceptor());
    app.useGlobalFilters(new HttpExceptionFilter());
    app.use(json({ limit: '50mb' }));
    app.use(urlencoded({ limit: '50mb', extended: true }));
    
    await app.listen(4500);
    
    console.log(`应用已启动: ${process.env.ASSETS_URL}`);
    console.log(`FILE_URL ${process.env.FILE_PATH}`);
    console.log(`TEST_URL ${process.env.TEST_URL}`);
}

bootstrap();